<template>
  <ContentWrap>
    <fromTitle title="承检机构管理" :hasPermi="'qis:organization-qualification:query'"  :queryParams="queryParams" @search="getList"  @reset="resetQuery" /> 

    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >


        <el-form-item label="机构名称" prop="name" >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入机构名称"
          clearable
          @keyup.enter="handleQuery"
           class="!w-240px"
        />
      </el-form-item>


  <el-form-item  label="统一社会信用代码" label-width="150px" prop="unifiedSocialCreditIdentifier">
        <el-input
          v-model="queryParams.unifiedSocialCreditIdentifier"
          placeholder="请输入统一社会信用代码"
          clearable
          @keyup.enter="handleQuery"
         class="!w-240px"
        />
      </el-form-item>


  
      <!-- <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>

      </el-form-item> -->
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <div class="table-btn">
      <el-button type="warning" v-hasPermi="['qis:check-organization:create']" @click="openForm('create')">新增</el-button>
    <el-button type="info" v-hasPermi="['qis:check-organization:export']" @click="handleExport">导出</el-button>
    <el-button type="danger" v-hasPermi="['qis:check-organization:delete']"  @click="handleDelete">删除</el-button>
    </div>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column width="55">
        <template #header>
          <span>选择</span>
        </template>
        <template #default="scope">
          <el-checkbox v-model="scope.row.selected" @change="handleSelect(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="机构名称" align="center" prop="name" >
        <template #default="scope">
          <el-link type="primary" @click="handleDetail('update',scope.row.id)">{{ scope.row.name }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="机构地址" align="center" prop="address" />
      <el-table-column label="法人单位名称" align="center" prop="legalPersonCompany" />
      <el-table-column label="统一社会信用代码" align="center" prop="unifiedSocialCreditIdentifier" />
      <el-table-column label="机构管理员" align="center" prop="manager" />

    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { OrganizationQualificationApi, OrganizationQualificationVO } from '@/api/qis/organizationqualification'
import { useRouter } from 'vue-router'
import { number } from 'vue-types'

// 扩展类型定义
interface ExtendedOrganizationQualificationVO extends OrganizationQualificationVO {
  selected?: boolean
}

/** 质检平台-承检资质 列表 */
defineOptions({ name: 'OrganizationQualification' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const router = useRouter()

const loading = ref(false) // 列表的加载中
const list = ref<ExtendedOrganizationQualificationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

// 处理选择逻辑
// const handleSelectionChange = (rows: ExtendedOrganizationQualificationVO[]) => {
//   console.log(rows,'rows')
//   rows.forEach(row => {
//     if (!row.hasOwnProperty('selected')) {
//       row.selected = false
//     }
//   })
// }

const arrayList = ref([])
const handleSelect = (row ) => {
  // console.log('删除指标要求', row)
  if(row.selected){
    arrayList.value.push(row.id)
    console.log(arrayList.value,'arrayList.value')
  }else {
    arrayList.value = arrayList.value.filter(item => item !== row.id)
    console.log(arrayList.value,'arrayList.value')
  }

  console.log(arrayList.value,'arrayList.value')
}




const selectArrayItem = ref<number[]>([]) // 选中的数组
const ids = ref<string>('')
const SelectionChange = async (row)=>{
    console.log(row)
    selectArrayItem.value.push(row.id)
    console.log(selectArrayItem.value,'selectArrayItem')
     ids.value =selectArrayItem.value.join(';')
  
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await OrganizationQualificationApi.getOrganizationQualificationPage(queryParams)
    list.value = data.list
    // handleSelectionChange(list.value)
    list.value.forEach(item => {
      item.selected = false
    })
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const openForm = (type:string,id?:Number) => {
  router.push({path:'/inspection-info-management/organization-qualification/add',query:{type:type,id:id}})
}


const handleDetail = (type:string,id:number)=>{
  router.push({path:'/inspection-info-management/organization-qualification/add',query:{type:type,id:id}})
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    if(arrayList.value.length > 0){
      ElMessageBox.confirm('确定删除选中数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
      const idsArr = arrayList.value.join(';')   
          // 发起删除
    await OrganizationQualificationApi.deleteOrganizationQualification(idsArr)
    arrayList.value = []
    message.success(t('common.delSuccess'))
    // 刷新列表

     getList()
    }).catch(() => {
    })
    }
    
    else {
      message.error('请选择要删除的数据')
    }

  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  if(total.value > 10000 ) return message.warning('当前数据过大，请筛选数据后导出 ')

  try {
    // 导出的二次确认
    if(arrayList.value.length > 0){
      const idsArr = arrayList.value.join(';')   
      await message.exportConfirm()
    // 发起导出
    console.log('111')
    loading.value = true
    exportLoading.value = true
    const data = await OrganizationQualificationApi.exportCjjgGl({ids:idsArr,...queryParams})
    download.excel(data, '质检平台-承检机构管理.xls')
    } else{
      await message.exportConfirm()
    // 发起导出
    console.log('111')
    exportLoading.value = true
    loading.value = true
    const data = await OrganizationQualificationApi.exportCjjgGl(queryParams)
    download.excel(data, '质检平台-承检机构管理.xls')
    }

  } catch {
    
  } finally {
    exportLoading.value = false
    loading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})

onUnmounted(()=>{
  queryParams.pageNo = 1
  queryFormRef.value.resetFields()

}) 

onDeactivated(()=>{
  queryParams.pageNo = 1
  queryFormRef.value.resetFields()
})

onActivated(()=>{
  queryParams.pageNo = 1
  getList()
})
</script>


<style lang="scss" scoped>

.table-btn{
  padding: 10px 50px;
}
</style>